// Check: when we have a pointer p to an object o which contains other
// pointers, say q(s), then is it safe to delete p without destroying
// q(s)?

#include <iostream>

struct Child
{
  std::string name;

  Child(const std::string n)
    : name(n)
  { }
};

struct Parent
{
  std::string name;
  Child* child;

  Parent(const std::string n, Child* c)
    : name(n), child(c)
  { }

  Parent(const std::string n)
    : name(n), child(new Child("unknown name"))
  { }
};


int main()
{
  Child*  c1 = new Child("Marry");
  Parent* p1 = new Parent("John", c1);

  Parent* p2 = new Parent("Jane");
  Child*  c2 = p2->child;

  std::cout << "Before deleting: " << std::endl
	    << "p1 = " << p1->name << ", " << p1->child->name << std::endl
	    << "p2 = " << p2->name << ", " << p2->child->name << std::endl;

  delete p1;
  p1 = 0;

  delete p2;
  p2 = 0;

  std::cout << "After deleting, check whether c1 and c2 are still here: " << std::endl
	    << "c1 = " << c1->name << std::endl
	    << "c2 = " << c2->name << std::endl;
}
